﻿Imports Training.Common

Partial Public Class CreateUserAccount
    Inherits System.Web.UI.Page
    Dim gid As Integer ' groupid

    ''' <summary>
    ''' Page Load event check for querystring data
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNothing(Request.QueryString("gid")) Then
            gid = Request.QueryString("gid")
        Else
            Response.Redirect("Login.aspx")
        End If
    End Sub

    ''' <summary>
    ''' Check login name and email for duplicates
    ''' </summary>
    ''' <returns>Error Message(s)</returns>
    ''' <remarks>use before attempting to create membership account</remarks>
    Private Function CheckItems() As String
        Dim retVal As String

        ' set inital value
        retVal = ""

        ' check username
        If CheckLoginName(tbLogin.Text) > 0 Then
            retVal &= " Login name is in use, please enter a different login name. "
        End If

        ' check email
        If CheckMemberEmail(tbEmail.Text) > 0 Then
            retVal &= " Email address is in use, please enter a different Email address."
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Create membership login and add to Users table
    ''' </summary>
    ''' <returns>Returns membership message and error code</returns>
    ''' <remarks>Checks prior to this function should prevent errors</remarks>
    Private Function CreateUser() As String
        Dim retVal As String
        Dim uCount As Integer

        ' create membership account
        retVal = AddGroupLogin(tbLogin.Text, tbPass.Text, tbEmail.Text)

        ' add user to "user" role
        Roles.AddUserToRole(tbLogin.Text, "user")

        ' lookup userid for user to associate login with user table
        Dim user As MembershipUser = Membership.GetUser(tbLogin.Text)
        Dim uid As String = user.ProviderUserKey.ToString()

        ' add user and get iError code
        uCount = AddGroupUser(uid, tbFname.Text, tbLname.Text, gid, tbComp.Text, tbAdr.Text, tbCity.Text, _
                              ddlState.SelectedValue, tbZip.Text, tbPhone.Text, _
                              tbEmail.Text, tbSiteCode.Text, 1)
        If uCount > 0 Then
            retVal &= " Error adding User. Error has been logged."
        Else
            retVal &= " Your individual account is now active. Logout and log back in as you<br />to access the training materials and quizzes"
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Clear the session on loguot
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub LoginStatus2_LoggedOut(ByVal sender As Object, ByVal e As EventArgs)
        Session.Abandon()
    End Sub


    ''' <summary>
    ''' Create Login Button click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>run checks before adding account</remarks>
    Private Sub btnCreateLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateLogin.Click
        If Page.IsValid Then
            ' run checks
            ' check for existing elements of group & membership
            If Len(CheckItems()) <> 0 Then
                ' display error msg
                lblAlert.Text = CheckItems()
            Else
                ' checks passed, proceed with account creation
                lblAlert.Text = CreateUser() ' AddUserFunction
            End If
        Else
            ' display no validation error
            lblAlert.Text = "All fields could not be validated. Please try again."
        End If
    End Sub
End Class